local SRC = "/home/xavier/fedora/Download/gtalarge_stream_h264copyfs_large.mp4"
local DST = "/home/xavier/dev/temp/dev/wow-jerry-stuff/luaav/test.mpg"
local av = require"av"

local function test_read()
	local file = av.open_format_context(SRC)
	print(file, file.filename)
	local codecs = {}
	local src_frame_size, dst_frame_size
	local save, sws

	for i = 1, file.nb_streams do
		local stream = file:get_stream(i)
		local codec = stream.codec
		print(stream.index, codec.codec_type, codec.codec_id, codec.bit_rate, codec.flags, codec.time_base)
		if codec.codec_type == "video" then
			print("\t", codec.width, codec.height, codec.me_method, codec.pix_fmt)
			save = av.new_codec_context()
			save.codec_type = "video"
			save.width = codec.width
			save.height = codec.height
			save.pix_fmt = "rgb32"
			src_frame_size = av.calc_frame_size(codec.pix_fmt, codec.width, codec.height)
			dst_frame_size = av.calc_frame_size(save.pix_fmt, save.width, save.height)
			print(src_frame_size, codec.pix_fmt, codec.width, codec.height)
			print(dst_frame_size, save.pix_fmt, save.width, save.height)
			sws = av.new_sws_context(codec.width, codec.height, codec.pix_fmt, save.width, save.height, save.pix_fmt)
		elseif codec.codec_type == "audio" then
			print("\t", codec.sample_rate, codec.channels, codec.sample_fmt)
		end
		decoder = av.find_decoder(codec.codec_id)
		codec:open(decoder)
		codecs[stream.index] = codec
	end

	local pkt = av.new_packet()

	local encoder = av.find_encoder("png")
	save:open(encoder)
	local src_frame = av.new_frame()
	local dst_frame = av.new_frame()
	dst_frame:alloc_picture(save.pix_fmt, save.width, save.height)

	local handle = function ()
		if not file:read_frame(pkt) then return end
		local codec = codecs[pkt.stream_index]
		if codec.codec_type == "video" then
			if codec:decode_video(pkt.data, src_frame) then
				print("frame", src_frame.display_picture_number, src_frame.coded_picture_number)
				sws:scale(src_frame, dst_frame, 0, codec.height)
				local pkt = save:encode_video(dst_frame)
				local f = io.open(string.format("frame_%d.png", src_frame.coded_picture_number), "wb")
				f:write(pkt.data)
				f:close()
			end
		end
		return true
	end
	while handle() do end

	for _, codec in pairs(codecs) do codec:close() end
end

local function test_write()
	local file = av.new_format_context(DST)
	local o = av.guess_format(nil, DST)
	file.oformat = o
	print(o.name, o.long_name, o.mime_type, o.video_codec, o.audio_codec, o.flags())
	local global_header = o.flags.globalheader

	local vs = file:new_stream(0)
	local c = vs.codec
	c.codec_type = "video"
	c.codec_id = o.video_codec
	c.bit_rate = 400000
	c.width = 352
	c.height = 288
	c.time_base = 1 / 25
	c.gop_size = 12
	c.pix_fmt = "yuv420p"
	c.flags.global_header = global_header

	local as = file:new_stream(1)
	c = as.codec
	c.codec_type = "audio"
	c.codec_id = o.audio_codec
	c.bit_rate = 64000
	c.sample_rate = 44100
	c.channels = 2
	c.flags.global_header = global_header

	print("here")
	file:write_header()
	file:write_trailer()
	print("there")
	file:close()
end

test_read()
--~ test_write()
